package com.glb.gilibuy.web.admin;

import com.glb.gilibuy.entity.UserOrder;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yupeng Zhang
 * @Date: 2021-08-12 10:33
 */
@Api(tags = "管理员订单管理模块")
@Controller
public class AdminOrderController extends BaseController {
    @ApiOperation(value = "订单物流更新", notes = "订单物流更新", produces = "application/json")
    @PostMapping("/admin/addTrackingNumber")
    @ResponseBody
    public Map<String, Object> addTrackingNumber(@RequestParam("orderId") String orderId,
                                                 @RequestParam("trackingNumber") String trackingNumber) {
        if (StringUtils.isEmpty(orderId)) {
            return MapGenerator.getRes(500, "请输入商品编号！");
        }
        if (StringUtils.isEmpty(trackingNumber)) {
            return MapGenerator.getRes(500, "请输入物流单号！");
        }
        if (orderService.addTrackingNumberToOrder(orderId, trackingNumber) > 0) {
            return MapGenerator.getRes(200, "物流单号添加成功！");
        }
        return MapGenerator.getRes(500, "添加物流单号失败！");
    }

    @ApiOperation(value = "获取所有订单", notes = "获取所有订单", produces = "application/json")
    @PostMapping("/admin/showAllOrder")
    @ResponseBody
    public Map<String, Object> showAllOrder() {
        List<UserOrder> orderList = orderService.findALLOrder();
        if (orderList.isEmpty()) {
            return MapGenerator.getRes(500, "没有查到订单！");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("orderList", orderList);
        return MapGenerator.getResWithData(200, "查看订单列表信息成功！", data);
    }

    @ApiOperation(value = "修改订单状态、支付类型、快递单号", notes = "修改订单状态、支付类型、快递单号", produces = "application/json")
    @PostMapping("/admin/updateOrder")
    @ResponseBody
    public Map<String, Object> updateOrder(@RequestBody UserOrder userOrder) {
        if (StringUtils.isEmpty(userOrder.getOrderId())) {
            return MapGenerator.getRes(500, "要修改的订单id不能为空！");
        }
        return orderService.updateOrder(userOrder);
    }

}
